package com.zjlanyun.erp;

import java.util.ArrayList;
import java.util.HashMap;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.aihook.pulltorefresh.lib.XListView;

public class Warning extends Activity implements XListView.IXListViewListener {

	private XListView list;
	private TextView title;
	private LinearLayout ll_back;
	private ArrayList<HashMap<String, Object>> mList;
	private SimpleAdapter mSimpleAdapter;
	private Handler mHandler;
	private Context mContext = this;
	private int count = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.warning);

		initView();
	}

	// 初始化View
	@SuppressLint("NewApi")
	private void initView() {
		title = (TextView) findViewById(R.id.title);
		list = (XListView) findViewById(R.id.list);
		ll_back = (LinearLayout) findViewById(R.id.ll_back);

		title.setText("预警信息");

		// 设置列表数据
		mList = new ArrayList<HashMap<String, Object>>();
		list.setPullLoadEnable(true);
		list.setOverScrollMode(View.OVER_SCROLL_NEVER);
		list.setRefreshTime("刚刚");
		mSimpleAdapter = new SimpleAdapter(this, getData(), R.layout.warning_item,
				new String[] { "billtype", "datetime", "billnumber", "projectname" },
				new int[] { R.id.billtype, R.id.datetime, R.id.billnumber, R.id.projectname });
		list.setAdapter(mSimpleAdapter);
		mHandler = new Handler();
		list.setXListViewListener(this);
		list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				Intent intent = new Intent(mContext, WarningDetail.class);
				intent.putExtra("billtype", mList.get(arg2 - 1).get("billtype").toString());
				intent.putExtra("trantype", mList.get(arg2 - 1).get("trantype").toString());
				startActivity(intent);
			}
		});

		// 后退
		ll_back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
	}

	// 获取列表数据
	private ArrayList<HashMap<String, Object>> getData() {
		count++;
		if (count > 4)
			list.setPullLoadEnable(false);
		String[] trantype = { "73", "302", "704", "1", "2", "21" };
		String[] billtype = { "采购申请单", "出货申请单", "收货通知单", "采购入库单", "产品入库单", "销售出库单" };
		String[] datetime = { "2015-05-09 10:20", "2015-06-09 10:20", "2015-07-09 10:20", "2015-08-10 10:20",
				"2015-09-12 10:50", "2015-10-20 21:20" };
		String[] billnumber = { "PO201510-001", "PO201510-002", "PO201510-003", "PO201510-004", "PO201510-005",
				"PO201510-006" };
		String[] projectname = { "某某项目名称", "某某项目名称", "某某项目名称", "某某项目名称", "某某项目名称", "某某项目名称" };
		for (int i = 0; i < billtype.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("billtype", billtype[i]);
			map.put("datetime", datetime[i]);
			map.put("billnumber", billnumber[i]);
			map.put("trantype", trantype[i]);
			map.put("projectname", projectname[i]);
			mList.add(map);
		}
		return mList;
	}

	/** 停止刷新， */
	private void onLoad() {
		list.stopRefresh();
		list.stopLoadMore();
		list.setRefreshTime("");
	}

	// 刷新
	@Override
	public void onRefresh() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				mList.clear();
				getData();
				list.setAdapter(mSimpleAdapter);
				onLoad();
			}
		}, 1200);
	}

	// 加载更多
	@Override
	public void onLoadMore() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				getData();
				mSimpleAdapter.notifyDataSetChanged();
				onLoad();
			}
		}, 500);
	}

}
